package com.intel.two.controller.community;

import com.intel.two.common.LayuiPageVo;
import com.intel.two.common.R;
import com.intel.two.pojo.community.Pet;
import com.intel.two.service.community.PetService;
import com.intel.two.utils.OssUtil;
import com.intel.two.vo.community.petBarVo;
import com.intel.two.vo.community.petPieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("pet")
public class PetController {
    @Autowired
    private PetService petService;

    /*查全部+分页*/
    @RequestMapping("/petList")
    public LayuiPageVo<Pet> petPageData(@RequestParam Map<String, Object> params) {
        System.out.println(params);
        return petService.petPageData(params);
    }

    /*增加*/
    @PostMapping("/addPet")
    public R addPet(Pet pet) {
        System.out.println(pet);
        int result = petService.addPet(pet);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    @PostMapping("/uploadPic")
    public R uploadPic(MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String fileUrl = OssUtil.updateLoad(file.getInputStream(), originalFilename);
        return R.ok().data("url",fileUrl);
    }



    @GetMapping("/deletePet")
    public R deletePet(Integer pet_id) {
        int result = petService.deletePet(pet_id);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("/deletePetAll")
    public R deletePetAll(@RequestParam Map<String, Object> map) {
        int result = petService.deletePetAll(map);
        if (result > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    @PostMapping("updatePet")
    public R updatePet(Pet pet) {
        System.out.println(pet);
        int result = petService.updatePet(pet);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/petBar")
    public R petBar(){
        List<petBarVo> list = petService.petBar();
        return R.ok().data("typeBar",list);
    }
    @RequestMapping("/petPie")
    public R petPie(){
        List<petPieVo> list = petService.petPie();
        return R.ok().data("typePic",list);
    }
}